package com.penghaisoft.microwms.producthand.web.controller.outwarehouse;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.base.model.BaseFactory;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProSellOut;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProSellOutDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IBaseFactoryService;
import com.penghaisoft.microwms.product.service.IProHandInService;
import com.penghaisoft.microwms.product.service.IProSellOutService;

/**
 * @Description 成品销售出库
 * copy@luoteng wzhg
 * @date 2018年6月18日
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/pro/sell/outwh")
public class ProOutWhController extends BaseController {

	@Autowired
	private IProHandInService proHandInService;
	
	@Autowired
	private IBaseDictionaryService baseDictionaryService;
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;//注入对象

	@Autowired
	private IProSellOutService proSellOutService;
	
	@Autowired
	private IBaseFactoryService baseFactoryService;
	
	/** 
	 * @Title confirm
	 * @Description 提单
	 * @copy luoteng @edit wzhg
	 * @date 2018年6月14日:上午10:15:19
	 * @param proSellOut
	 * @return HandResp
	 */
	@PostMapping("loadBill")
	public HandResp confirm(@RequestBody ProSellOut proSellOut) {
		HandResp result = new HandResp();
		Resp resp = proSellOutService.loadBill(proSellOut);
		result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;
	}
	
	/** 
	 * @Title info
	 * @Description 详情
	 * @copy luoteng @edit wzhg
	 * @date 2018年6月14日:上午10:55:29
	 * @param proSellOut
	 * @return HandResp
	 */
	@PostMapping("info")
	public HandResp info(@RequestBody ProSellOut proSellOut) {
		HandResp result = new HandResp();

		proSellOut.setActiveFlag("1");
		List<ProSellOut> list = proSellOutService.queryListByAny(proSellOut);

		// 字典表获取型号类型的中文名
		BaseDictionary baseDictionary1 = new BaseDictionary();
		baseDictionary1.setActiveFlag("1");
		baseDictionary1.setDicType(Constant.MODEL_TYPE_P);
		List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);

		if(list != null && !list.isEmpty()){
			for(ProSellOut tmpData : list){
				if(list1 != null && !list1.isEmpty()){
					for(BaseDictionary tmp : list1){
						if(tmp.getDicCode().equals(tmpData.getModelType())){
							tmpData.setModelTypeCn(tmp.getDicName());
						}
					}
				}
			}
		}

		result.setCode(Constant.SUCCESS);
		result.setList(list);
		return result;
	}
	
	/** 
	 * @Title scan
	 * @Description 扫码
	 * @copy luoteng @edit wzhg
	 * @date 2018年6月14日:上午11:08:57
	 * @param proSellOutDto
	 * @return HandResp
	 */
	@PostMapping("scan")
	public HandResp scan(@RequestBody ProSellOutDto proSellOutDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_SELLOUT_PREFIX + proSellOutDto.getBarcode();
		
		int validRange = 0;
		
		BaseFactory baseFactory = new BaseFactory();
		baseFactory.setFactoryCode("1001");
		baseFactory.setActiveFlag("1");
		List<BaseFactory> fList = baseFactoryService.queryByAny(baseFactory);
		if(fList == null || fList.isEmpty()){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前工厂配置存在问题，请联系管理员！");
			return resp;
		}else{
			BaseFactory tmp = fList.get(0);
			if(tmp.getUserDefined2() == null || tmp.getUserDefined2().equals("")){
				resp.setCode(Constant.FAIL);
				resp.setMsg("当前工厂配置内发货质检有效期参数存在问题，请联系管理员！");
				return resp;
			}else{
				validRange = Integer.parseInt(tmp.getUserDefined2());
			}
		}

		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

			TransResult result = new TransResult();
			try{
				result = proSellOutService.sellOutScanBarcode(proSellOutDto, validRange);
				resp.setCode(result.getCode());
				resp.setMsg(result.getMsg());
				resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}

		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
	
	/** 
	 * @Title del
	 * @Description 删除条码
	 * @copy luoteng @edit wzhg
	 * @date 2018年6月14日:下午5:10:45
	 * @param proSellOutDto
	 * @return HandResp
	 * pro/sell/outwh/scansc
	 */
	@PostMapping("scansc")
	public HandResp del(@RequestBody ProSellOutDto proSellOutDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_SELLOUT_PREFIX + proSellOutDto.getBarcode();
		
		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("当前条码正在处理中，请不要重复扫描！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			
			TransResult result = new TransResult();
			try{
				result = proSellOutService.sellOutDelBarcode(proSellOutDto);
				resp.setCode(result.getCode());
		        resp.setMsg(result.getMsg());
		        resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}
		
		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
	
	/** 
	 * @Title complete
	 * @Description 确认完成
	 * @copy luoteng @edit wzhg
	 * @date 2018年6月14日:下午5:54:53
	 * @param proSellOutDto
	 * @return HandResp
	 */
	@PostMapping("complete")
	public HandResp complete(@RequestBody ProSellOutDto proSellOutDto) {
		HandResp resp = new HandResp();
		String key = Constant.PRO_SELLOUT_PREFIX + proSellOutDto.getLoginName();

		if(stringRedisTemplate.hasKey(key)){
			resp.setCode(Constant.FAIL);
			resp.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
			return resp;
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

			TransResult result = new TransResult();
			try{
				result = proSellOutService.sellOutComplete(proSellOutDto);
				resp.setCode(result.getCode());
				resp.setMsg(result.getMsg());
				resp.setList(result.getList());
			}catch(RuntimeException e){
				resp.setCode(Constant.FAIL);
				resp.setMsg(e.getMessage());
				stringRedisTemplate.delete(key);//删除对象
			}
		}

		stringRedisTemplate.delete(key);//删除对象
		return resp;
	}
}
